package com.soxing.braindata.service.sync;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.soxing.braindata.dao.entity.Role;
import com.soxing.braindata.dao.entity.UserInfo;
import com.soxing.braindata.dao.entity.UserInfoExt;
import com.soxing.braindata.dao.service.IRoleService;
import com.soxing.braindata.dao.service.IUserInfoExtService;
import com.soxing.braindata.dao.service.IUserInfoService;
import com.soxing.braindata.domain.enumeration.FunTypeEnum;
import com.soxing.braindata.domain.response.sync.SyncResponse;
import com.soxing.braindata.domain.rpc.OriginRole;
import com.soxing.braindata.domain.rpc.OriginUser;
import com.soxing.common.utils.util.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 同步用户
 */
@Slf4j
@Service("syncUserService")
public class SyncUserServiceImpl implements ISyncService<OriginUser> {
    @Resource
    private IUserInfoService userInfoService;
    @Resource
    private IRoleService roleService;
    @Resource
    private IUserInfoExtService userInfoExtService;

    @Override
    public SyncResponse syncData(OriginUser originUser) {
        try {
            Integer funType = originUser.getFunType();
            String userPkId = originUser.getUserPkId();
            if (funType == null || userPkId == null) {
                log.error("funType或subjectPkId为空。funType:{},userPkId:{}", funType, userPkId);
                return SyncResponse.fail("funType或userPkId为空");
            }
            sync(originUser);
            return SyncResponse.success();
        } catch (Exception e) {
            log.error("同步课题服务异常", e);
            return SyncResponse.fail();
        }
    }


    @Transactional
    public void sync(OriginUser originUser) {
        UserInfo userInfo = createUser(originUser);
        userInfoService.remove(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUserNum, userInfo.getUserNum()));
        Integer funType = originUser.getFunType();
        if (funType == FunTypeEnum.DELETE.getFunType()) {
            return;
        }
        userInfoService.save(userInfo);

        UserInfoExt userInfoExt = new UserInfoExt();
        userInfoExt.setUserNum(userInfo.getUserNum());
        userInfoExt.setUserPwd("8ae01bd48fdb918855763baac63b19ac");
        userInfoExt.setUserType(1);
        Date now = new Date();
        userInfoExt.setModifyTime(now);
        userInfoExtService.save(userInfoExt);

        List<Role> roleList = originRole2Role(originUser.getRoleList());
        for(Role role:roleList){
            role.setUserPkId(userInfo.getUserNum());
            if(role.getType()==null){
                role.setType(0);
            }
        }
        if (!CollectionUtils.isEmpty(roleList)) {
            roleService.remove(new LambdaQueryWrapper<Role>().eq(Role::getUserPkId, originUser.getUserAccount()));
            roleService.saveBatch(roleList);
        }
    }

    /**
     * OriginRole->Role
     *
     * @param originRoleList
     * @return
     */
    private List<Role> originRole2Role(List<OriginRole> originRoleList) {
        List<Role> roleList = Lists.newArrayList();
        if (CollectionUtils.isEmpty(originRoleList)) {
            return roleList;
        }
        originRoleList.forEach(originRole -> {
            roleList.add(ObjectMapper.map(originRole, Role.class));
        });
        return roleList;
    }


    private UserInfo createUser(OriginUser originUser) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserNum(originUser.getUserAccount());
        userInfo.setUserPkId(originUser.getUserPkId());
        userInfo.setUserName(originUser.getUserName());
        userInfo.setUserSex(originUser.getUserSex());
        userInfo.setUserPhone(originUser.getUserPhone());
        userInfo.setUserEmail(originUser.getUserEmail());
        userInfo.setUserPost(originUser.getUserPost());
        userInfo.setJobNum(originUser.getJobNum());
        userInfo.setAgencyCode(originUser.getAgencyCode());
        userInfo.setAgencyName(originUser.getAgencyName());
        if(originUser.getType()==null){
            userInfo.setType(0);
        }else{
            userInfo.setType(originUser.getType());
        }
        return userInfo;
    }
}
